package com.wym.lottery.ui;

import java.util.ArrayList;
import java.util.List;

import com.wym.lottery.adapter.DataArrayAdapter;
import com.wym.lottery.model.Ticket;
import com.wym.lottery.service.BaseService;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class SellFragment extends Fragment {

	private StringBuffer strNum = new StringBuffer();
	private DataArrayAdapter adapter;

	private TextView show_input_number_panel;

	private ListView listView;
	private View tablepad;
	private Button button;
	private Button bt_delete;

	private static int sectionID;
	private BaseService.MyBinder mbinder;

	public static SellFragment newInstance(int sectionNumber) {
		sectionID = sectionNumber;
		SellFragment sellFragment = new SellFragment();
		return sellFragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_sell, container, false);

		List<Integer> buttonBlock = new ArrayList<Integer>();
		buttonBlock.add(R.id.dial_Num_0);
		buttonBlock.add(R.id.dial_num_1);
		buttonBlock.add(R.id.dial_num_2);
		buttonBlock.add(R.id.dial_num_3);
		buttonBlock.add(R.id.dial_num_4);
		buttonBlock.add(R.id.dial_num_5);
		buttonBlock.add(R.id.dial_num_6);
		buttonBlock.add(R.id.dial_num_7);
		buttonBlock.add(R.id.dial_num_8);
		buttonBlock.add(R.id.dial_num_9);
		buttonBlock.add(R.id.come_num);
		buttonBlock.add(R.id.fix_num);
		buttonBlock.add(R.id.split_line);

		buttonBlock.add(R.id.split_num);

		for (Integer integer : buttonBlock) {
			rootView.findViewById(integer).setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					appendInputNumber(v);
					pushData2Panel();
				}
			});
		}

		listView = (ListView) rootView.findViewById(R.id.show_database_data);

		adapter = new DataArrayAdapter(getActivity(), R.layout.simple_item, new ArrayList<Ticket>());
		listView.setAdapter(adapter);

		tablepad = rootView.findViewById(R.id.tablepad);
		button = (Button) rootView.findViewById(R.id.show_input_panel);
		// 打开输入盘
		rootView.findViewById(R.id.show_input_panel).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				tablepad.setVisibility(View.VISIBLE);
				button.setVisibility(View.INVISIBLE);

			}
		});
		// 关闭输入盘
		rootView.findViewById(R.id.panel_down).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				tablepad.setVisibility(View.INVISIBLE);
				button.setVisibility(View.VISIBLE);

			}
		});

		// 提交
		rootView.findViewById(R.id.submit_num).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mbinder = ((MainActivity) getActivity()).binder;
				boolean flag = false;
				if (strNum.indexOf("#") == -1) {
					Ticket t = mbinder.checkInputData(strNum.toString());
					if (t != null) {
						inflateList(t);
						mbinder.submitData(t);
						flag = true;
					}
				} else {
					List<Ticket> ts = mbinder.checkInputDatum(strNum.toString());
					if (ts.isEmpty() != true)
						for (Ticket t : ts) {
							inflateList(t);
							mbinder.submitData(t);
							flag = true;
						}
				}
				// 如果都不执行上述操作，就show一个提示信息
				if (flag == false)
					Toast.makeText(getActivity(), "输入格式错误", Toast.LENGTH_SHORT).show();
				//清空显示板
				clearShowDataPanel();
			}
		});

		return rootView;
	}

	// 将单击的数字加入StringBuffer中
	private void appendInputNumber(View v) {
		strNum.append(v.getTag().toString().trim());
	}

	// 把数字展现到显示框中
	private void pushData2Panel() {
		show_input_number_panel.setText(strNum);
	}

	// 清空显示框中的数据
	private void clearShowDataPanel() {
		strNum.setLength(0);// 清空数据
		pushData2Panel();
	}

	private void inflateList(Ticket ticket) {
		adapter.insert(ticket, 0);

	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.sell_menu_panel, menu);
		if (menu.findItem(R.id.show_input_num) != null) {
			View view = menu.findItem(R.id.show_input_num).getActionView();
			show_input_number_panel = (TextView) view.findViewById(R.id.memu_tv_show_input_num);
			bt_delete = (Button) view.findViewById(R.id.memu_tv_delete_input_num);
			bt_delete.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (strNum.length() != 0)
						strNum.deleteCharAt(strNum.length() - 1);
					pushData2Panel();
				}
			});
			bt_delete.setLongClickable(true);
			bt_delete.setOnLongClickListener(new View.OnLongClickListener() {

				@Override
				public boolean onLongClick(View v) {
					clearShowDataPanel();
					return false;
				}
			});
		}
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {

		super.onActivityCreated(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public void onAttach(Activity activity) {
		((MainActivity) activity).onSectionAttached(sectionID);
		super.onAttach(activity);
	}
}
